﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ImpactTenis.AcessoADados.DTOs;
using System.Configuration;
using System.Data.SqlClient;

namespace ImpactTenis.AcessoADados.Usuarios
{
    public class UsuarioDados
    {
        private string _connectionString {get; set;}

        public UsuarioDados()
        {
            _connectionString = ConfigurationManager.ConnectionStrings["ImpactTenis"].ConnectionString;
        }

        public UsuarioDTO ObterUsuario(int funcional)
        {
            try
            {
                
                using (var conexao = new SqlConnection(_connectionString))
                {
                    string commandText = "SELECT * FROM tbl_Usuario where Funcional = @Funcional";
                    var command = new SqlCommand(commandText, conexao);

                    var pmtFuncional = command.CreateParameter();
                    pmtFuncional.ParameterName = "@Funcional";
                    pmtFuncional.SqlDbType = System.Data.SqlDbType.Int;
                    pmtFuncional.Value = funcional;

                    command.Parameters.Add(pmtFuncional);

                    UsuarioDTO usuario = new UsuarioDTO();
                    conexao.Open();
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            usuario.Id = Convert.ToInt32(reader["Id"].ToString());
                            usuario.Funcional = Convert.ToInt32(reader["Funcional"].ToString());
                            usuario.Senha = reader["Senha"].ToString();
                            usuario.Nome = reader["Nome"].ToString();
                            usuario.TipoUsuario = Convert.ToInt32(reader["IdTipoUsuario"].ToString());
                        }
                    }
                    return usuario;
                }
            }
            catch

            {
                throw new Exception("Não foi possível obter os dados do usuário");
            }


        }
    }
}
